<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>倒计时</title>
</head>

<body>
    <h1>倒计时</h1>
</body>
<script>
    function countDown(time) {
        let nowTime = +new Date(); //返回当前时间总的毫秒数
        let targetTime = +new Date(time) //返回的是 用户输入时间的总毫秒数
        let times = (targetTime - nowTime) / 1000; // times是剩余的总秒数
        let d = parseInt(times / 60 / 60 / 24);
        d = d < 10 ? '0' + d : d; //天
        let h = parseInt(times / 60 / 60 % 24);
        h = h < 10 ? '0' + h : h; //时
        let m = parseInt(times / 60 % 60);
        m = m < 10 ? '0' + m : m; //分
        let s = parseInt(times % 60);
        s = s < 10 ? '0' + s : s; //秒
        return `${d}天${h}时${m}分${s}秒`;
    }
    console.log(countDown('2022-5-1 00:00:00'));
</script>

</html>